﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TicTacToe
{
    public class TicTacToe
    {
        //1 circle
        //-1 cross
        //0, empty
        public bool IsEnd(int[][] ss)
        {
            bool isEnd;
            bool isColOK, isRowOK;
            int nCircle, nCross;

            if (ss == null) throw new Exception("Snapshot should not be null");
            if (ss.Length != 3) throw new Exception("Snapshot dimension is not correct");
            nCircle = nCross = 0;
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    if (ss[i][j] == 1)
                        nCircle++;
                    else if (ss[i][j] == -1)
                        nCross++;
                    else if (ss[i][j] != 0)
                    {
                        throw new Exception("Illegal value in the snapshot");
                    }
                }
            if (nCircle - nCross > 1 || nCross - nCircle > 1)
                throw new Exception("Illegal pattern in the snapshot");
            if (nCircle + nCross <= 4) { isEnd = false; return isEnd; }

            isEnd = false;
            for (int i = 0; i < 3; i++)
            {
                isColOK = true;
                if (ss[i][0] != 0 && ss[i][1] == ss[i][0] && ss[i][2] == ss[i][0]) isColOK = false;

                isRowOK = true;
                if (ss[0][i] != 0 && ss[1][i] == ss[0][i] && ss[2][i] == ss[0][i]) isRowOK = false;

                if (isColOK && isRowOK) throw new Exception("Illegal pattern in the snapshot");
                if (isColOK || isRowOK)
                {
                    if (isEnd) throw new Exception("Illegal pattern in the snapshot");
                    isEnd = true;
                }
            }

            if (ss[0][0] != 0 && ss[1][1] == ss[0][0] && ss[2][2] == ss[0][0])
            {
                if (isEnd) throw new Exception("Illegal pattern in the snapshot");
                isEnd = true;
            }

            if (ss[2][0] != 0 && ss[1][1] == ss[2][0] && ss[0][2] == ss[2][0])
            {
                if (isEnd) throw new Exception("Illegal pattern in the snapshot");
                isEnd = true;
            }

            return isEnd;
        }

        public bool IsLeapYear(int year)
        {
            bool isLeap;

            isLeap = false;
            if (year % 4 == 0)
            {
                if (year % 100 == 0)
                {
                    if (year % 400 == 0)
                    {
                        isLeap = true;
                    }
                }
                else
                {
                    isLeap = true;
                }
            }


            return isLeap;
        }
    }
}
